/*
 * @FileName: BusinessValidator.java
 * @Author
 * @Description:
 * @Date 2016年1月11日 上午11:10:36
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.validator.admin;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.model.Organization;

/**
 * Class description goes here
 *
 * @author
 */
public class OrganizationValidator extends Validator{

    final static String PARENTNAME_MESSAGE = "parentnameMessages";
    final static String DISPLAYNAME_MESSAGE = "DisplayNameMessages";
    final static String NUMCODE_MESSAGE = "NumCodeMessages";
    final static String DISPLAYORDER_MESSAGE = "DisplayOrderMessages";
    final static String BUSINESS_MESSAGE = "BusinessMessages";

    @Override
    protected void validate(Controller c){
        if(!Organization.dao.checkUnique(c.getParaToInt(Business.ID), Organization.NUMCODE,
                                         c.getPara(Organization.NUMCODE))){
            addError(NUMCODE_MESSAGE, "机构编号已存在");
        }
        validateRequiredString(Organization.PARENTID, PARENTNAME_MESSAGE, "请选择上级机构");
        validateRequiredString(Organization.DISPLAYNAME, DISPLAYNAME_MESSAGE, "请输入机构名称");
        validateRequiredString(Organization.NUMCODE, NUMCODE_MESSAGE, "请输入机构编号");
        validateRequiredString(Organization.DISPLAYORDER, DISPLAYORDER_MESSAGE, "请输入排序数字");
        // validateRequiredString(Organization.BUSINESS, BUSINESS_MESSAGE,
        // "请输入主要业务");
        validateRegex(Organization.NUMCODE, Constant.Regex.ALPHABETS_NUMBERS, NUMCODE_MESSAGE, "请输入机构编号");
        validateRegex(Organization.DISPLAYORDER, Constant.Regex.NUMBER, DISPLAYORDER_MESSAGE, "请输入排序数字");
    }

    @Override
    protected void handleError(Controller c){
        c.setAttr("pager", Organization.dao.preview(c.getPara(Organization.ID, "")));
        c.keepPara();
        c.render("/admin/organization_edit.tpl");
    }
}
